package com.jinzhi.jzweb.service.resourceLibrary.impl;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.plugins.Page;
import com.jinzhi.jzweb.dao.resourceLibrary.EventResourceStatisticMapper;
import com.jinzhi.jzweb.domain.vo.resourceLibrary.eventresourceVO.*;
import com.jinzhi.jzweb.service.resourceLibrary.EventResourceStatisticService;
import com.jinzhi.sys.domain.UserDO;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @ClassName EventResourceStatisticServiceImp
 * @Description:
 * @Author: ak
 * @CreateDate: 2024/5/13 11:35
 * @UpdateUser:
 * @UpdateDate: 2024/5/13 11:35
 * @UpdateRemark:
 * @Version: 1.0
 */
@Service("EventResourceStatisticService")
public class EventResourceStatisticServiceImp implements EventResourceStatisticService {
    @Resource
    private EventResourceStatisticMapper statisticMapper;


    /**
     * @auther: ak
     * @Description 班级学生练习记录
     * @param:
     * @return:
     * @date: 2024/4/23 9:04
     */
    @Override
    public PageResult list(Long classId, Integer pageNum, Integer pageSize) {

        Page<PracticeRecordListVO> page = new Page<PracticeRecordListVO>(pageNum, pageSize);
        List<PracticeRecordListVO> practiceRecordList = statisticMapper.practiceRecordList(classId);

        return new PageResult(practiceRecordList.size(), practiceRecordList);
    }

    /**
     * @auther: ak
     * @Description 学生个人练习记录详情
     * @param:
     * @return:
     * @date: 2024/4/22 17:28
     */
    @Override
    public Page practiceRecordDetails(Long userId, Integer pageNum, Integer pageSize) {

        //  具体学生信息
        Page<PracticeRecordDetailsVO> page = new Page<PracticeRecordDetailsVO>(pageNum, pageSize);
        List<PracticeRecordDetailsVO> detailsVOList = statisticMapper.practiceDetailList(userId);
        return page.setRecords(detailsVOList);
    }

    /**
     * @auther: ak
     * @Description 班级学习记录统计
     * @param:
     * @return:
     * @date: 2024/4/23 9:28
     */
    @Override
    public Page listLearnStatistic(Long classId, Integer pageNum, Integer pageSize) {
        Page<LearnTimeStatisticListVO> page = new Page<LearnTimeStatisticListVO>(pageNum, pageSize);
        List<LearnTimeStatisticListVO> learnTimeList = statisticMapper.learnTimeList(classId);
        return page.setRecords(learnTimeList);
    }

    /**
     * @auther: ak
     * @Description 查看学生个人学习详情
     * @param:
     * @return:
     * @date: 2024/4/23 10:16
     */
    @Override
    public Page learnDetails(Long useId, Integer type, Integer pageNum, Integer pageSize) {
        Long evnMoId = 0L;

        // 对比类型 获取资源类型
        if (type == 0) {
            // 课程资源 金职大讲堂
            evnMoId = 1775402307172499457L;
        } else if (type == 1) {
            // 赛项资源
            evnMoId = 1775402446075265026L;
        } else if (type == 2) {
            // 1+x证书资源
            evnMoId = 1775402520624824321L;
        } else if (type == 4) {
            // 行业资源
            evnMoId = 1775402630247153666L;
        } else if (type == 3) {
            // 技能培训资源
            evnMoId = 1775402577172430850L;
        } else {
            return null;
        }

        // userId allocationId 获取资源名称 学习时间 学习时长
        Page<LearnTimeStatisticDetailVO> page = new Page<>(pageNum, pageSize);
        List<LearnTimeStatisticDetailVO> learnRecordsDaoList = statisticMapper.personalLearnTimeList(useId, evnMoId);

        List<LearnTimeStatisticDetailVO> collect = learnRecordsDaoList.stream().map(learnTimeStatisticDetailVO -> {
            LearnTimeStatisticDetailVO statisticDetailVO = new LearnTimeStatisticDetailVO();
            BeanUtils.copyProperties(learnTimeStatisticDetailVO, statisticDetailVO);
            statisticDetailVO.setLatestTime(String.valueOf(learnTimeStatisticDetailVO.getLatestTime().indexOf(17)));
            return statisticDetailVO;
        }).collect(Collectors.toList());

        page.setSize(collect.size());
        return page.setRecords(collect);
    }

    @Override
    public List<HomeResourceVO> getHomeResourceList() {
        List<EventresourceAllocationDO> allocationList = statisticMapper.getAllocationList();
        List<ModuleDao> moduleList = statisticMapper.getModuleList();

        // 首页资源总概
        ArrayList<HomeResourceVO> homeResourceVOList = new ArrayList<>();

        for (ModuleDao moduleDao : moduleList) {
            Long moduleId = moduleDao.getId();
            HomeResourceVO homeResourceVO = new HomeResourceVO();
            BeanUtils.copyProperties(moduleDao, homeResourceVO);

            ArrayList<EventresourceAllocationDO> allocationDOS = new ArrayList<>();
            for (EventresourceAllocationDO allocationDO : allocationList) {
                Long jzEveMoId = allocationDO.getJzEveMoId();
                if (moduleId.equals(jzEveMoId)) {
                    allocationDOS.add(allocationDO);
                }
            }

            homeResourceVO.setResourceList(allocationDOS);
            homeResourceVOList.add(homeResourceVO);
        }
        return homeResourceVOList;
    }

    @Transactional
    @Override
    public boolean deleteRecord() {
        boolean learn = statisticMapper.deleteLearnRecord();
        boolean practice = statisticMapper.deletePracticeRecord();
        return (learn == true && practice == true) ? true : false;
    }

    @Override
    public ExamPaperVO getExamPaper(Long practiceRecordId) {
        ExamPaperVO examPaperVO = new ExamPaperVO();
        PracticeRecordDO practiceRecord = statisticMapper.getContinuePracticeRecord(practiceRecordId);
        BeanUtils.copyProperties(practiceRecord, examPaperVO);
        LocalDateTime latestPracticeTime = practiceRecord.getLatestPracticeTime();
        examPaperVO.setLatestPracticeTime(latestPracticeTime.getYear() + "-" + latestPracticeTime.getMonthValue() + "-" + latestPracticeTime.getDayOfMonth());
        examPaperVO.setTemplateName(practiceRecord.getPaperTitle());
        examPaperVO.setPracticeRecordId(practiceRecord.getId());

        // string转list
        JSONArray answerJson = JSON.parseArray(practiceRecord.getQuestionAnswerList());
        List<ItemIdOptionBody> questionVOList = JSONArray.parseArray(answerJson.toJSONString(), ItemIdOptionBody.class);
        // 获取题目详情 存放用户输入答案
        ArrayList<ItemPaperDao> single = new ArrayList<>();
        ArrayList<ItemPaperDao> multiple = new ArrayList<>();
        ArrayList<ItemPaperDao> judge = new ArrayList<>();
        ArrayList<ItemPaperDao> practical = new ArrayList<>();
        for (ItemIdOptionBody item : questionVOList) {
            String option = item.getValue();
            ItemPaperDao itemPaperDao = statisticMapper.questionDetail(item.getId());
            itemPaperDao.setOption(option);
            // 正确答案改变输出样式
            itemPaperDao.setRightKey(itemPaperDao.getRightKey().replaceAll(";;", ","));
            // 判断题目类型
            if (itemPaperDao.getTopicTypes().equals(0)) {
                single.add(itemPaperDao);
            } else if (itemPaperDao.getTopicTypes().equals(1)) {
                multiple.add(itemPaperDao);
            } else if (itemPaperDao.getTopicTypes().equals(2)) {
                judge.add(itemPaperDao);
            } else if (itemPaperDao.getTopicTypes().equals(3)) {
                practical.add(itemPaperDao);
            }
        }

        examPaperVO.setSingle(single);
        examPaperVO.setMultiple(multiple);
        examPaperVO.setJudge(judge);
        examPaperVO.setPractical(practical);
        examPaperVO.setTemplateId(practiceRecord.getPaperId());

        return examPaperVO;
    }
}
